package com.northwest_man.refactor_tool;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

// 找出其它语言比英文多出来的 key
public class KeyWithoutEnglish {
    private static final String en = "en";

    public static void main(String[] args) {
        final String moduleAbsPath = "D:\\Projects\\oraimo-android\\module_resource";
        final String valuesRelPath = "\\src\\main\\res\\values";

        String[] languages = {"zh", "en", "fr", "ar", "pt", "bn"}; // 语言列表

        String[] xmlFiles = new String[languages.length];
        for (int i = 0; i < languages.length; i++) {
            final String lang = languages[i];
            if ("en".equals(lang)) {
                xmlFiles[i] = String.format("%s%s\\strings.xml", moduleAbsPath, valuesRelPath);

            } else {
                xmlFiles[i] = String.format("%s%s-%s\\strings.xml", moduleAbsPath, valuesRelPath, lang);
            }
        }


//        String outputFileName = "output.xlsx"; // 生成的Excel文件名

        try {


            Map<String, Map<String, String>> localizationData = new HashMap<>();

            for (int i = 0; i < languages.length; i++) {
                String language = languages[i];
                String xmlFile = xmlFiles[i];

                Map<String, String> kv = parseXML(xmlFile);
                localizationData.put(language, kv);
            }

            {// 先把英文加上
                final String lang = en;
                Map<String, String> kv = localizationData.get(lang);
                int rowIndex = 1;


            }
            // 英文 key 数组
            final Object[] enKeyArray = localizationData.get(en).keySet().toArray();
            final Map<String, Integer> enKeyIndex = new HashMap<>();
            // 英文行数
            final int enRowCount = enKeyArray.length;
            for (int i = 0; i < enRowCount; i++) {

                enKeyIndex.put((String) enKeyArray[i], i + 1);
            }
            final StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < languages.length; i++) {
                final String lang = languages[i];

                Map<String, String> kv = localizationData.get(lang);


                for (final String k : kv.keySet()) {

                    final Integer enKeyRow = enKeyIndex.get(k);
                    if (enKeyRow == null) {
                        stringBuilder.append(k).append("\n");
                        continue;
                    }


                }
                System.out.printf("%s\n%s", lang, stringBuilder);
            }

//            FileOutputStream fileOut = new FileOutputStream(outputFileName);
//            workbook.write(fileOut);
//            fileOut.close();

            System.out.println("Excel file generated successfully!");

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static Map<String, String> parseXML(String xmlFile) throws Exception {
        Map<String, String> languageData = new HashMap<>();

        File file = new File(xmlFile);
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(file);

        doc.getDocumentElement().normalize();

        NodeList nodeList = doc.getElementsByTagName("string");

        for (int i = 0; i < nodeList.getLength(); i++) {
            Element element = (Element) nodeList.item(i);
            String name = element.getAttribute("name");
            String value = element.getTextContent();
            languageData.put(name, value);
        }

        return languageData;
    }


}
